package com.shxc.smarttrade.kvb.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.kvb.model.QuestionsModel;


import org.xchris.view.annotation.ViewInject;
import org.xchris.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 调查问卷 - 答案
 */
public class AnswersAdapter extends BaseAdapter {
    private Context c;
    private Handler h;
    private List<QuestionsModel.DataBean.AnswersListBean> list;
    private int chooseId = -1;

    public AnswersAdapter(Context c, List<QuestionsModel.DataBean.AnswersListBean> list, Handler handler) {
        this.c = c;
        h = handler;
        this.list = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            this.list.add(list.get(i));
        }
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHoledr holedr = null;
        if (convertView == null) {
            convertView = View.inflate(c, R.layout.item_kvb_answer, null);
            holedr = new ViewHoledr();
            x.view().inject(holedr, convertView);//ViewHolder的注释
            convertView.setTag(holedr);
        } else {
            holedr = (ViewHoledr) convertView.getTag();
        }
        final QuestionsModel.DataBean.AnswersListBean model = list.get(position);

        holedr.textview.setText(model.getValue());

        if (chooseId==model.getId()) {
            holedr.textview.setBackgroundColor(c.getResources().getColor(R.color.kvb_account));
            holedr.textview.setTextColor(Color.WHITE);
        }else{
            holedr.textview.setBackgroundColor(c.getResources().getColor(R.color.kvb_answer));
            holedr.textview.setTextColor(c.getResources().getColor(R.color.kvb_text_title));
        }

        holedr.textview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseId = model.getId();
                notifyDataSetChanged();

                Message msg = new Message();
                msg.what = 0;
                msg.obj = model;
                h.sendMessage(msg);
            }
        });

        return convertView;
    }

    static class ViewHoledr {
        @ViewInject(R.id.textview)
        TextView textview;//内容文字
    }
}
